/**
 * @CallFunctions.as
 *
 * @author sodaChen mail:asframe@qq.com
 * @version 1.0
 * <br>Copyright (C), 2012 ASFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:ASFrame EasyPlane
 * <br>Date:2012-11-5
 */
package com.asframe.fun
{
	import flash.utils.Dictionary;

	/**
	 * 对Function数组进行回调
	 * @author sodaChen
	 * Date:2012-11-5
	 */
	public class CallFunMap
	{
		public var funCount:int;
		protected var funDic:Dictionary;

		private var removeFunList:Vector.<Function>;
		private var addFunList:Vector.<Function>;

		private var isRunning:Boolean;

		private var tempFun:Function;

		public function CallFunMap()
		{
			funDic = new Dictionary();
			removeFunList = new Vector.<Function>();
			addFunList = new Vector.<Function>();
		}
		public function getCount():int
		{
			return funCount;
		}
		public function addFunction(fun:Function):void
		{
			if(isRunning)
			{
				addFunList.push(fun);
				return ;
			}
			funDic[fun] = fun;
			funCount++;
		}
		public function removeFunction(fun:Function):void
		{
			if(isRunning)
			{
				removeFunList.push(fun);
				return ;
			}
			funDic[fun] = null;
			delete funDic[fun];
			funCount--;
		}
		public function clear():void
		{

		}
		public function calls():void
		{
			isRunning = true;
			for each (tempFun in funDic)
			{
				tempFun();
			}
			isRunning = false;
			if(removeFunList.length > 0)
			{
				for each (tempFun in removeFunList)
				{
					removeFunction(tempFun);
				}
				removeFunList.length = 0;
			}
			if(addFunList.length > 0)
			{
				for each (tempFun in addFunList)
				{
					addFunction(tempFun);
				}
				addFunList.length = 0;
			}
		}
	}
}
